package com.personnel.service.impl;

import com.personnel.common.MessageConstant;
import com.personnel.common.Result;
import com.personnel.common.StatusCode;
import com.personnel.mapper.LoginMapper;
import com.personnel.pojo.Staff;
import com.personnel.service.LoginService;
import com.personnel.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 登录服务实现类
 *
 * @author yuepeng
 * @since 2021-11-25 19:06:07
 * @version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginMapper loginMapper;

    @Override
    public Result login(Staff staff, HttpSession session) {
        Staff returnStaff = loginMapper.login(staff.getLoginName(), MD5Util.getMD5String(staff.getPassword()));
        if(returnStaff != null){
            if(returnStaff.getAction() == 0){
                return new Result(false, StatusCode.LOGIN_ERROR,MessageConstant.USER_Action_ERROR);
            }
            //查询成功
            session.setAttribute("user", returnStaff);
            return new Result(true, StatusCode.OK,MessageConstant.SEARCH_USER_SUCCESS,returnStaff);
        }else {
            //查询失败
            return new Result(false, StatusCode.LOGIN_ERROR,MessageConstant.SEARCH_USER_ERROR);
        }
    }

    @Override
    public Result loginByPhone(Staff staff, HttpSession session) {
        Staff returnStaff = loginMapper.loginByPhone(staff.getPhone(), staff.getMsgCode());
        if(returnStaff != null){
            if(returnStaff.getAction() == 0){
                return new Result(false, StatusCode.LOGIN_ERROR,MessageConstant.USER_Action_ERROR);
            }
            //查询成功
            session.setAttribute("user", returnStaff);
            return new Result(true, StatusCode.OK,MessageConstant.SEARCH_USER_SUCCESS,returnStaff);
        }else {
            //查询失败
            return new Result(false, StatusCode.LOGIN_ERROR,MessageConstant.SEARCH_USER_ERROR);
        }
    }
}
